package Thread_lock;

import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/29
 * Time:19:12
 * 读写锁
 **/
public class Demo7 {
    public static void main(String[] args) {

        //创建一个读写锁
        //默认情况为非公平锁（false）
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        //读锁
        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
        //写锁
        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();

        //线程池

        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10, 10, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000)
        );
        //任务1：执行读锁
        executor.execute(new Runnable() {
            @Override
            public void run() {
                //加锁
                readLock.lock();
                try {
                    //业务逻辑处理
                    System.out.println(Thread.currentThread().getName() +
                            "执行了读锁操作：" + new Date());
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //释放锁
                    readLock.unlock();
                }
            }
        });

        //任务2：执行读锁
        executor.execute(new Runnable() {
            @Override
            public void run() {
                //加锁
                readLock.lock();
                try {
                    //业务逻辑处理
                    System.out.println(Thread.currentThread().getName() +
                            "执行了读锁操作：" + new Date());
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //释放锁
                    readLock.unlock();
                }
            }
        });
        //读锁操作的时间间隔<3 s,则说明为共享锁

        //任务3：执行写锁
        executor.execute(new Runnable() {
            @Override
            public void run() {
                //加锁
                writeLock.lock();
                try {
                    //业务逻辑处理
                    System.out.println(Thread.currentThread().getName() +
                            "执行了写锁操作：" + new Date());
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //释放锁
                    writeLock.unlock();
                }
            }
        });

        //任务4：执行写锁
        executor.execute(new Runnable() {
            @Override
            public void run() {
                //加锁
                writeLock.lock();
                try {
                    //业务逻辑处理
                    System.out.println(Thread.currentThread().getName() +
                            "执行了写锁操作：" + new Date());
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    //释放锁
                    writeLock.unlock();
                }
            }
        });

        //写锁操作的时间间隔>=3 s,则说明为独占锁（非共享锁）

    }
}
